package com.h819.core.web.jquery.dynamicform;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Set;
import java.util.TreeSet;

/**
 * @Title: DynamicFormServlet.java
 * @Description: TODO(动态表单操作，对应 jquery_examples\jquery-dynamic-form 目录下的文件)
 * @author H819
 * @date 2010-8-30
 * @version V1.0
 */
public class DynamicFormServlet extends HttpServlet {

	private Logger log = Logger.getLogger(DynamicFormServlet.class);

	/**
	 * Constructor of the object.
	 */
	public DynamicFormServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Enumeration<String> enu = request.getParameterNames();

		while (enu.hasMoreElements()) {
			String para = enu.nextElement().toString();
			// out.println(para + " :" + request.getParameter(para));
		}

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method " + " <br/>");
		enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String para = enu.nextElement().toString();
			out.println(para + " :" + request.getParameter(para) + " <br/>");
		}

		enu = request.getParameterNames();
		out.println("foodnum :"
				+ this.getDynamicFormValue(request, 0, "foodnum") + " <br/>");
		out.println("formSum :" + this.getDynamicFormSum(request) + " <br/>");

		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();

		// 可以带参数转发转发
		// RequestDispatcher dispatcher =
		// request.getRequestDispatcher("/jsp/foodadd_search.jsp");
		// dispatcher.forward(request, response);
	}

	/**
	 * The doPut method of the servlet. <br>
	 * 
	 * This method is called when a HTTP put request is received.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 * 获取动态表单参数值
	 * 
	 * @param request
	 *            表单请求
	 * @param formNum
	 *            表单序号
	 * @param parameter
	 *            请求参数
	 * @return 请求参数对应的值
	 */
	private String getDynamicFormValue(HttpServletRequest request, int formNum,
			String parameter) {
		Enumeration<String> enu = request.getParameterNames();
		String paraValue = "";
		String temp = "mainForm[mainForm][" + formNum + "][" + parameter + "]";
		// out.println("temp :"+temp);
		while (enu.hasMoreElements()) {
			String para = enu.nextElement().toString();
			// out.println(para);
			if (para.equals(temp)) {
				paraValue = request.getParameter(temp);
				// out.println(parameter+":"+paraValue);
				break;
			}
		}
		return paraValue;
	}

	/**
	 * 获取动态表单的总数
	 * 
	 * @param request
	 *            表单请求
	 * @return 表单总数
	 */
	private int getDynamicFormSum(HttpServletRequest request) {
		Enumeration<String> enu = request.getParameterNames();
		Set<String> s = new TreeSet<String>();
		String tag = "][";
		while (enu.hasMoreElements()) {
			String para = enu.nextElement().toString();
			if (StringUtils.countMatches(para, tag) == 2) {
				// out.println(para);
				String temp = StringUtils.substringBefore(StringUtils
						.substringAfter(para, tag), tag);
				// out.println(temp);
				s.add(temp);
			}
		}
		return s.size();
	}

}
